import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import 'src/hrpub/common/static/fonts/iconfont.css';

import {snCreateUIDom, getAppPageConfig} from '../../../../hrpub/common/utils/utils';

import {
    createPage,
    appUrlUtils
} from 'nc-lightapp-front';
import HomePage from '../container';
import pageIns from '../pageIns';


const {setAppUrl} = appUrlUtils;

if (/^localhost|127\.0\.0\.1/.test(window.location.hostname)) {
    setAppUrl('2019', 'c=60071010&p=60071010p');
}

let appInfo = getAppPageConfig()
const reqData = [{
    rqUrl: '/platform/templet/querypage.do',
    rqJson: `{\n  \"pagecode\": \"60071010p\",\n  \"appcode\": \"${appInfo.appcode}\"\n}`,
    rqCode: 'template'
}, {
    rqUrl: '/platform/templet/querypage.do',
    rqJson: `{\n  \"pagecode\": \"60092010batch\",\n  \"appcode\": \"60092010\"\n}`,
    rqCode: 'batch_add'
}, {
    rqUrl: '/platform/appregister/queryallbtns.do',
    rqJson: `{\n  \"pagecode\": \"60071010p\",\n  \"appcode\": \"${appInfo.appcode}\"\n}`,
    rqCode: 'button'
}, {
    rqUrl: '/platform/appregister/queryappcontext.do',
    rqJson: `{\n  \"appcode\": \"${appInfo.appcode}\"}`,
    rqCode: 'context'
}];


snCreateUIDom(appInfo, reqData, function (res) {
    let cardTableList = [];
    let billInfo = Object.keys(res.template['gridrelation']).map((key) => {
        if (key !== 'ncc60071701list') {
            cardTableList.push(key);
            return {
                billtype: 'card',
                pagecode: appInfo.appcode,
                bodycode: key,
                headcode: 'bd_psndoc',
                tabletype: 'cardTable'
            };
        } else {
            return {
                billtype: 'grid',
                pagecode: appInfo.appcode,
                bodycode: key
            };
        }

    });

    billInfo = [{
        billtype: 'form',
        pagecode: appInfo.appcode,
        headcode: 'bd_psndoc'
    }, {
        billtype: 'form',
        pagecode: appInfo.appcode,
        headcode: 'hi_psnjob_h',
    }, {
        billtype: 'form',
        pagecode: appInfo.appcode,
        headcode: 'hi_psnorg_h',
    }].concat(billInfo)

    const Wrapper = createPage({
        billinfo: billInfo
    })(HomePage);

    ReactDOM.render(<Wrapper ref={ref => pageIns.ins = ref} cardTableList={cardTableList} templateData={res}/>, document.getElementById('app'));
});
